<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payroll extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
	    	redirect('memberlogin/requiredmessage','refresh');
	    }
	    elseif ($_SESSION['role_id'] !=1){
    		redirect('memberlogin/restriction','refresh');
	    }
	    $data['menus'] = 'menus';
        //$data['staffs'] = $this->MStaff->getAllStaffActive();
        //$data['left'] = 'payroll/payroll_left';
		$this->load->vars($data);
    }
	    
	public function index()
	{
		$data['title'] = "Payroll - Friends-International Organization";
		$data['main'] = 'payroll/payrolllist';
        $data['payrolls'] = $this->MPayroll->getPayrollList();
		$this->load->vars($data);
		$this->load->view('template'); 
	}
    public function search()
    {
        if($this->input->post('datepaid')){ 
            $data['title'] = "Payroll - Friends-International Organization";
    		$data['main'] = 'payroll/payrolllist';
    		$data['payrolls'] = $this->MPayroll->getPayrollList($this->input->post('datepaid'));
    		$this->load->vars($data);
    		$this->load->view('template');   
        }else {
           redirect('payroll','refresh'); 
        }   
    }
    
    public function createPayroll()
    {
        if ($this->input->post('submit')){
	  		$this->MPayroll->createPayroll();
	  		$this->session->set_flashdata('message','Payroll created');
			redirect('payroll','refresh');
	  	}else{
			$data['title'] = "Add Staff Payroll";
			$data['main'] = 'payroll/payrolladd';
            $data['staffs'] = $this->MStaff->getStaffDropDown();
			$data['documenttypes'] = $this->MStaff->getDocumentTypeDropDown();	
            $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/payroll');
			$this->load->vars($data);
			$this->load->view('template');  
	  	}    
    }
    
    public function editPayroll($id=0)
    {
        if ($this->input->post('submit')){
	  		$this->MPayroll->updatePayroll();
	  		$this->session->set_flashdata('message','Payroll updated');
			redirect('payroll','refresh');
	  	}else{
			$data['title'] = "Edit Staff Payroll";
			$data['main'] = 'payroll/payrolledit';
            $data['staffs'] = $this->MStaff->getStaffDropDown();
			$data['documenttypes'] = $this->MStaff->getDocumentTypeDropDown();	
            $data['payroll'] = $this->MPayroll->getPayroll($id);
            $data['form'] = $this->MFormGlobal->getFormEditCancelBottom('/index.php/payroll');
			$this->load->vars($data);
			$this->load->view('template');  
	  	}    
    }
    
    public function viewPayrollDetail($id)
    {
        $data['title'] = "Payroll Details";
		$data['main'] = 'payroll/payrolldetail';
        $data['payroll'] = $this->MPayroll->getPayrollDetailByPayrollId($id);
        $data['form'] = $this->MFormGlobal->getFormEditCancel('/index.php/payroll/editPayroll/'.$id, '/index.php/payroll');
		$this->load->vars($data);
		$this->load->view('template');  
    }
}
